/*************************************************************************
 * Copyright 2008 David Almilli
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *      
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 **************************************************************************/
package com.tivo.hme.samples.bananas;

import com.tivo.hme.bananas.BApplicationPlus;
import com.tivo.hme.bananas.BEvent;
import com.tivo.hme.bananas.BMovableListPlus;
import com.tivo.hme.bananas.BText;
import com.tivo.hme.bananas.BView;
import com.tivo.hme.bananas.BMovableListPlus.MoveEvent;
import com.tivo.hme.bananas.layout.Layout;
import com.tivo.hme.bananas.layout.LayoutManager;
import com.tivo.hme.sdk.HmeEvent;

public class MovableListScreen extends BananasSampleScreen {
	private StandardList list;

	public MovableListScreen(BApplicationPlus app) {
		super(app);

        LayoutManager lm = new LayoutManager(getNormal());
        Layout safeTitle = lm.safeTitle(this);
        Layout layout = safeTitle;

        layout = lm.relativeY(layout);
        layout = lm.stretch(layout);
        
        list = new StandardList(getNormal(), layout);
        String [] s1 = {"This", "is", "a", "movable", "list","This", "is", "a", "movable", "list","This", "is", "a", "movable", "list"};
        list.add(s1);
        setFocusDefault(list);
	}
    
    /**
     * Since there is only one button, we will treat select like a left 
     * action and pop the screen.
     */
    public boolean handleKeyPress(int code, long rawcode) 
    {
        switch (code) {
        case KEY_SELECT:
        case KEY_LEFT:
            postEvent(new BEvent.Action(this, "pop"));
            getBApp().play("select.snd");
            return true;
        }
        return super.handleKeyPress(code, rawcode);
    }
    
    public boolean handleEvent(HmeEvent event) {
        if (event instanceof MoveEvent) {
            MoveEvent moveEvent = (MoveEvent)event;
            int start = moveEvent.getOldIndex();
            int end = moveEvent.getNewIndex();
            if (start > end) {
                start = moveEvent.getNewIndex();
                end = moveEvent.getOldIndex();
            }
            list.reloadRows(start, end);
            return true;
        }
        return super.handleEvent(event);
    }
    
    
    /**
     *  Title of the screen.
     */
    public String toString() 
    {
        return "BMovableList";
    }

    static class StandardList extends BMovableListPlus
    {
        /**
         * Constructor
         */
        public StandardList(BView parent, Layout layout)
        {
            super(parent, layout);
            setBarAndArrows(BAR_HANG, BAR_DEFAULT, "pop", null);
        }

        /**
         * Create a row that has an icon and text.
         */
        protected void createRow(BView parent, int index)
        {
            int indent = getRowIndent(BAR_DEFAULT, true) + 3;
            BView icon = new BView(parent, indent, 0, 34, parent.getHeight());
            icon.setResource("star.png");
            BText text = new BText(parent, indent + 40, 0, parent.getWidth()-(indent + 50), parent.getHeight());
            text.setFlags(RSRC_HALIGN_LEFT);
            text.setShadow(true);
            text.setColor(getColor());
            text.setFont(getFont());
            
            //
            // set the value of the row to be the text that was 
            // passed in through add()
            //
            
            text.setValue((index+1) + ". " + get(index));
        }        
    }

}
